package com.lizx.interceptor;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lizx.pojo.Employee;
import com.lizx.staff.service.EmployeeService;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class MyInterceptor implements HandlerInterceptor {

    EmployeeService employeeService;

    public MyInterceptor(EmployeeService employeeService){
        this.employeeService = employeeService;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

        String requestURI = request.getRequestURI(); // 获取请求路径
        if(requestURI.contains(".js") || requestURI.contains(".css") || requestURI.contains(".png")){
            return true;
        }

        if(requestURI.contains("staff")){ // 员工在请求，处理员工功能
//            if(requestURI.contains("employee/toLogin") || requestURI.contains("employee/login")){
//                return true;
//            }
            if(requestURI.contains("employee/toAdd") || requestURI.contains("staff/area/selectListAll")){
                return true;
            }

            if(requestURI.contains("employee/authorize")){
                return true;
            }
            if(requestURI.contains("employee/getCode")){
                return true;
            }
            if(requestURI.contains("employee/authorizeCheck")){
                return true;
            }

            if(requestURI.contains("employee/getOpenId")){
                return true;
            }

            if(requestURI.contains("employee/getInfo")){
                return true;
            }

            if(requestURI.contains("/staff/reimbursement/error")){
                return true;
            }

            if(requestURI.contains("/staff/employee/locks")){
                return true;
            }

            if(requestURI.contains("/staff/employee/noauthor")){
                return true;
            }

            String openId = (String) request.getSession().getAttribute("openId");
//            if(openId==null){
//                try {
//                    response.sendRedirect("/staff/reimbursement/error");
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }
//            }
            LambdaQueryWrapper<Employee> queryWapper = new LambdaQueryWrapper<Employee>();
            queryWapper.eq(Employee::getOpenid,openId);
            Employee employee = employeeService.getOne(queryWapper);
            if(employee==null){
                try {
                    response.sendRedirect("/staff/reimbursement/error");
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(employee!=null && employee.getStatus().equals("0")){
                try {
                    response.sendRedirect("/staff/employee/noauthor");
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(employee!=null && employee.getStatus().equals("2")){
                try {
                    response.sendRedirect("/staff/employee/locks");
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        if(requestURI.contains("admin")){ // 管理员在请求，处理管理员功能
            if(requestURI.contains("user/toLogin") || requestURI.contains("user/login")){
                return true;
            }
            if(requestURI.contains(".js") || requestURI.contains(".css") || requestURI.contains(".png")){
                return true;
            }
            Integer adminUserId = (Integer) request.getSession().getAttribute("adminUserId");
            if(adminUserId==null){
                try {
                    response.sendRedirect("/admin/user/toLogin");
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        System.out.println("postHandle: " + request.getRequestURI());
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        System.out.println("afterCompletion: " + request.getRequestURI());
    }
}
